﻿using Kingdee.BOS;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Msg;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RK.SerivesPlugin.BPW
{
    [Description("生产领料单保存时，校验是否是对应车间仓"), HotUpdate]
    public class PickStockIsOk : AbstractOperationServicePlugIn
    {
        public override void BeforeExecuteOperationTransaction(BeforeExecuteOperationTransaction e)
        {
            base.BeforeExecuteOperationTransaction(e);
            Context ctx = ObjectUtils.CreateCopy(this.Context) as Context;
            base.BeforeExecuteOperationTransaction(e);
            foreach (var obj in e.SelectedRows.Select(s => s.DataEntity))
            {
                if (obj == null) { return; }
                DynamicObjectCollection rows = obj["Entity"] as DynamicObjectCollection;
                foreach (var row in rows)
                {
                    DynamicObject workShop = row["WorkShopId"] as DynamicObject;
                    string wsName = workShop == null ? "" : workShop["FullName"].ToString();
                    DynamicObject stock = row["StockId"] as DynamicObject;
                    string stockName = stock == null ? "" : stock["Name"].ToString();
                    if (wsName.Contains("焊接车间"))
                    {
                        if (stockName != "焊接车间仓")
                        {
                            string message = $"车间:{wsName},与仓库:{stockName},不对应";
                            Base.AddOperationResult(this.OperationResult, false, message, MessageType.Normal, "车间仓校验", 0);
                            e.Cancel = true;
                        }
                    }
                    if (wsName.Contains("机加工车间"))
                    {
                        if (stockName != "机加工车间仓")
                        {
                            string message = $"车间:{wsName},与仓库:{stockName},不对应";
                            Base.AddOperationResult(this.OperationResult, false, message, MessageType.Normal, "车间仓校验", 0);
                            e.Cancel = true;
                        }
                    }
                    if (wsName.Contains("调整臂车间"))
                    {
                        if (stockName != "自动调整臂车间仓")
                        {
                            string message = $"车间:{wsName},与仓库:{stockName},不对应";
                            Base.AddOperationResult(this.OperationResult, false, message, MessageType.Normal, "车间仓校验", 0);
                            e.Cancel = true;
                        }
                    }
                    if (wsName.Contains("电泳车间"))
                    {
                        if (stockName != "电泳车间仓")
                        {
                            string message = $"车间:{wsName},与仓库:{stockName},不对应";
                            Base.AddOperationResult(this.OperationResult, false, message, MessageType.Normal, "车间仓校验", 0);
                            e.Cancel = true;
                        }
                    }
                    if (wsName.Contains("总装车间"))
                    {
                        if (stockName != "总装车间仓")
                        {
                            string message = $"车间:{wsName},与仓库:{stockName},不对应";
                            Base.AddOperationResult(this.OperationResult, false, message, MessageType.Normal, "车间仓校验", 0);
                            e.Cancel = true;
                        }
                    }
                }
            }
        }
    }
}

